package com.xiaoyu.lanling.feature.report.viewholders

import `in`.srain.cube.views.list.ViewHolderBase
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.facebook.drawee.view.SimpleDraweeView
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.event.report.ReportDeletePictureEvent
import com.xiaoyu.lanling.event.report.ReportPublishPreviewPictureClickEvent
import com.xiaoyu.lanling.feature.report.datamodels.ReportMediaImageItem
import com.xiaoyu.lanling.feature.report.datamodels.ReportMediaItemBase
import com.xiaoyu.lanling.media.image.ImageViewUtil

class ReportMediaImageViewHolder : ViewHolderBase<ReportMediaItemBase>() {

    private var mView: View? = null
    private var mImageView: SimpleDraweeView? = null
    private var mDeleteView: ImageView? = null

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        mView = layoutInflater.inflate(R.layout.moment_publish_image_item, parent, false)
        mImageView = mView?.findViewById(R.id.drawee_view)
        mDeleteView = mView?.findViewById(R.id.delete_view)

        mView?.setOnClickDebounceListener(sOnClickHandler)
        mDeleteView?.setOnClickDebounceListener(sDeleteOnClickHandler)

        return mView
    }

    override fun showData(position: Int, itemData: ReportMediaItemBase) {
        val item = itemData as ReportMediaImageItem
        ImageViewUtil.loadImage(mImageView, item.imageLoadParam)
        mView?.setViewTag(item)
        mDeleteView?.setViewTag(item)
    }

    companion object {

        private val sOnClickHandler = View.OnClickListener { v ->
            val item = v.getViewTag<ReportMediaImageItem>()
                    ?: return@OnClickListener
            ReportPublishPreviewPictureClickEvent(item).post()
        }

        private val sDeleteOnClickHandler = View.OnClickListener { v ->
            val item = v.getViewTag<ReportMediaImageItem>()
                    ?: return@OnClickListener

            ReportDeletePictureEvent(item.position, item.path).post()
        }
    }
}
